目前整體遊戲架構接近完整了,接著開始做一些遊戲優化,讓整個遊戲體驗更流暢。
介紹 clamp
Variant clamp(value: Variant, min: Variant, max: Variant)
Clamps the value, returning a Variant not less than min and not more than max. Any values that can be compared with the less than and greater than operators will work.
這個方法可以幫我們限制住數值保持在設定的最小值及最大值之間。
var player_size:Vector2
var player_offset_x
var player_offset_y
func _ready():
# ...
player_size = $CollisionShape2D.shape.size
player_offset_x = player_size.x/2
player_offset_y = player_size.y/2
紅色:player_size.x
藍色:player_size.y
紫色:player.position 能移動到的邊界。
粉紅:player_offset_x
水藍:player_offset_y
func update_pos(player):
# 先取得原始位移後位置。
var pos = player.position + direction*speed
# 限制 x 軸最小只能到角色的一半寬,最大只能到畫面寬減去角色一半寬。
pos.x = clamp(pos.x, player_offset_x, get_viewport_rect().size.x - player_offset_x)
# 限制 y 軸最小只能到角色的一半高,最大只能到畫面寬減去角色一半。
pos.y = clamp(pos.y, player_offset_y, get_viewport_rect().size.y - player_offset_y)
# 正式更新位置。
player.position = pos
process
中。func _process(delta):
match game_state:
GAME_STATE.START:
if dragged:
update_pos(player)
handle_player_state()
芫荽 / iansui
字體示範。
release
中選擇 zip 檔案下載。.tff
檔,將檔案放到 fonts
資料夾中。HUD
場景中選擇 label
與 button
節點,在右邊屬性面板中 ThemeOverrides
中 Fonts
點擊下拉選單選擇載入,選擇我們的字體檔案。角色檔案修改及新增的整合
extends CharacterBody2D
# ...
# player shape
var player_size:Vector2
var player_offset_x
var player_offset_y
# Called when the node enters the scene tree for the first time.
func _ready():
# ...
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
match game_state:
GAME_STATE.START:
if dragged:
update_pos(player)
handle_player_state()
func _input(event):
# ...
func handle_player_state():
# ...
# handle state
func handle_invincible_state():
# ...
func _on_invincible_timeout():
# ...
func handle_slow_state():
# ...
func _on_slow_timeout():
# ...
func handle_stop_state():
# ...
func _on_stop_timeout():
# ...
func handle_end_state():
# ...
func set_shader_para( mode: int, color: Vector4 =Vector4.ZERO, speed: float = 0):
# ...
# game state
func start():
# ...
func stop():
# ...
func end():
# ...
# player move logic
func update_pos(player):
var pos = player.position + direction*speed
pos.x = clamp(pos.x, player_offset_x, get_viewport_rect().size.x - player_offset_x)
pos.y = clamp(pos.y, player_offset_y, get_viewport_rect().size.y - player_offset_y)
player.position = pos
:)